package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BusinessException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageBean;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //菜品分页查询
    @Override
    public PageBean findByPage(DishPageDTO dishPageDTO) {
        //1. 开启分页
        PageHelper.startPage(dishPageDTO.getPage(), dishPageDTO.getPageSize());
        //2. 执行查询
        List<DishVO> dishVOList = dishMapper.findList(dishPageDTO);
        Page<DishVO> page = (Page<DishVO>) dishVOList;
        //3. 返回结果
        return new PageBean(page.getTotal(), page.getResult());
    }

    //菜品新增
    @Transactional//开启声明式事务
    @Override
    public void save(DishDTO dishDTO) {
        //1. 从dto中提取菜品基本信息,保存到dish表
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        dishMapper.save(dish);

        //2-1. 从dto中提取菜品口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();

        //2-2. 遍历列表,得到每个口味
        if (CollectionUtil.isNotEmpty(flavors)) {
            flavors.stream().forEach(
                    dishFlavor -> {
                        //2-3. 为每个口味设置好dish_id
                        dishFlavor.setDishId(dish.getId());
                        //2-4 执行保存
                        dishFlavorMapper.save(dishFlavor);
                    });
        }
    }

    //回显菜品->根据id查菜品
    @Override
    public DishVO findById(Long id) {
        //1. 根据菜品id查询菜品和分类信息
        DishVO dishVO = dishMapper.findById(id);

        //2. 根据菜品id去口味表查询口味列表
        List<DishFlavor> dishFlavors = dishFlavorMapper.findByDishId(id);

        //3.组装返回结果
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    //修改菜品
    @Transactional//开启声明式事务->涉及到口味表和菜品表,多表操作,同时成功或失败,开启事务
    @Override
    public void update(DishDTO dishDTO) {
        //1. 从dto中提取到菜品的基本信息,动态SQL更新
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        dishMapper.update(dish);
        //2. 先删除当前菜品在口味表中的所有记录
        dishFlavorMapper.deleteByDishId(dish.getId());
        //3. 从dto中提取到新的口味列表,重新保存到口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (CollectionUtil.isNotEmpty(flavors)) {
            flavors.stream().forEach(dishFlavor -> {
                //2-3. 为每个口味设置好dish_id
                dishFlavor.setDishId(dish.getId());
                //2-4 执行保存
                dishFlavorMapper.save(dishFlavor);
            });
        }
    }

    //批量删除菜品
    @Transactional//开启声明式事务->涉及到口味表和菜品表,多表操作,同时成功或失败,开启事务
    @Override
    public void deleteByIds(List<Long> ids) {
        //1. 遍历ids
        if (CollectionUtil.isNotEmpty(ids)) {
            ids.stream().forEach(id -> {
                //2. 根据id查询菜品信息
                DishVO dishVO = dishMapper.findById(id);

                //3. 判断状态,如果是起售,抛异常
                if (dishVO.getStatus() == 1) {
                    throw new BusinessException("要删除的菜品正在售卖,不能删除");
                }

                //todo 4. 判断当前菜品是否被套餐关联,如果是,直接抛异常

                //5. 根据菜品id从dish表中删除
                dishMapper.deleteById(id);

                //6. 根据菜品id从口味表删除
                dishFlavorMapper.deleteByDishId(id);
            });
        }
    }

    //菜品的起售and禁售
    @Override
    public void startOrStop(Dish dish) {
        dishMapper.update(dish);
    }

    //根据分类id或name查询菜品
    @Override
    public List<DishVO> findList(DishPageDTO dishPageDTO) {
        List<DishVO> list = dishMapper.findList(dishPageDTO);
        //遍历list,得到每个菜品,然后再去查询它的口味列表
        if (CollectionUtil.isNotEmpty(list)){
            list.stream().forEach(dishVO -> {
                List<DishFlavor> dishFlavors = dishFlavorMapper.findByDishId(dishVO.getId());
                dishVO.setFlavors(dishFlavors);
            });
        }
        return list;
    }

}